<?php
defined('_JEXEC') or die('Restricted access');
require_once('DTOEvent.php');
require_once('DTOEventComment.php');
require_once('DTOEventSuscription.php');
require_once('DTOEventCategory.php');
require_once('DTOAreaAdministrativa.php');
require_once('DTOPais.php');
require_once(JPATH_COMPONENT.DS.'models'.DS.'helpers'.DS.'geocoder.php');

class DTOFactory{
	private static $instance;
	public static function getInstance() 
	    {
	        if (!isset(self::$instance)) {
	            $c = __CLASS__;
	            self::$instance = new $c;
	        }
	        return self::$instance;
	}
	
	private function __construct(){
		
	}

	public function createDTOEvent(){
		return new DTOEvent();
	}
	public function createDTOEventComment(){
		return new DTOEventComment();
	}
	public function createDTOEventSuscription(){
		return new DTOEventSuscription();
	}
	public function createDTOAreaAdministrativa(){
		return new DTOAreaAdministrativa();
	}
	public function createDTOPais(){
		return new DTOPais();
	}
	public function createDTOEventCategory(){
		return new DTOEventCategory();
	}

	public function getDTOEventFromRequest(){
		$dtoEvent = $this->createDTOEvent();
		$geocoder=new SRGeocoder();
		$xml=$geocoder->reverseGeocode(JRequest::getVar('position',null));
		$dtoAreaAdmin=$geocoder->getDTOAreaAdmin($xml);
		$dtoEvent->setIdCreator ( JFactory::getUser()->id );
		$dtoEvent->setCategory ( JRequest::getVar('eventCategory',null) );
		$dtoEvent->setAreaAdmin ($dtoAreaAdmin);
		$dtoEvent->setStatus ('Sin comenzar');
		$dtoEvent->setNameEvent ( JRequest::getVar('eventName',null) );
		$dtoEvent->setDescription ( JRequest::getVar('eventDescrip',null) );
		$dtoEvent->setImage ( JRequest::getVar('eventImage',null) );
		$latLon=explode(',',JRequest::getVar('position',null));
		$lat = $latLon[0];
		$long = $latLon[1];
		$dtoEvent->setCoord ( '<Point><coordinates> '.$long.','.$lat.' </coordinates></Point>' );
		//TODO guarda 0 como fecha
		$dtoEvent->setDate ( JRequest::getVar('eventDate',null));
		$dtoEvent->setContadorVisitas ( 0 );
		$dtoEvent->setHora ( JRequest::getVar('eventHour',null) );
		return $dtoEvent;
	}
	
	public function fromDTOEvent2EventTable($evento){
		$row =&JTable::getInstance('evento','Table');
		
		if ($evento->getIdEvent())
			$row->idEvento = $evento->getIdEvent();			
		$row->numVisitas = $evento->getContadorVisitas();
		$row->idCreador = $evento->getIdCreator();
		$row->idCategoria = $evento->getCategory();
		$row->coordenada = $evento->getCoord();
		$row->idAreaAdministrativa = $evento->getAreaAdmin()->getIdArea();
		$row->fecha = $evento->getDate();
		$row->hora = $evento->getHora();
		$row->descripcionEvento = $evento->getDescription();
		$row->imagenEvento = $evento->getImage();
		$row->nombreEvento = $evento->getNameEvent();
		$row->estado = $evento->getStatus();
		
		return $row;
	}
	public function fromPaisTable2DTOPais($pais){
		$dtoPais = $this->createDTOPais();
		$dtoPais->setIdPais($pais->idPais);
		$dtoPais->setNamePais($pais->nombrePais);
		
		return $dtoPais;
	}
	public function fromDTOPais2PaisTable($pais){
		$row =&JTable::getInstance('pais','Table');
		
		$row->idPais = $pais->getIdPais();
		$row->nombrePais = $pais->getNamePais();

		return $row;
	}
	public function fromAreaAdminTable2DTOAreaAdmin($areaAdmin){
		$dtoAreaAdministrativa = $this->createDTOAreaAdministrativa ();
		$dtoAreaAdministrativa->setIdArea($areaAdmin->idAreaAdministrativa);
		$dtoAreaAdministrativa->setNameArea($areaAdmin->nombreAreaAdministrativa);
		
		return $dtoAreaAdministrativa;
	}
	public function fromDTOAreaAdmin2AreaAdminTable($areaAdmin){
		$row =&JTable::getInstance('areaadministrativa','Table');
		
		$row->idAreaAdministrativa=$areaAdmin->getIdArea();
		$row->nombreAreaAdministrativa=$areaAdmin->getNameArea();
		$row->idPais=$areaAdmin->getPais()->getIdPais();
		
		return $row;
	}
	public function fromEventTable2DTOEvent($evento) {
		$dtoEvent = $this->createDTOEvent ();
		$dtoAreaAdmin=$this->createDTOAreaAdministrativa();
		$dtoEvent->setIdEvent ( $evento->idEvento );
		$dtoEvent->setIdCreator ( $evento->idCreador );
		$dtoEvent->setCategory ( $evento->idCategoria );
		$dtoAreaAdmin->setIdArea($evento->idAreaAdministrativa);
		$dtoEvent->setAreaAdmin ( $dtoAreaAdmin );
		$dtoEvent->setStatus ( $evento->estado );
		$dtoEvent->setNameEvent ( $evento->nombreEvento );
		$dtoEvent->setDescription ( $evento->descripcionEvento );
		$dtoEvent->setImage ( $evento->imagenEvento );
		$dtoEvent->setCoord ( $evento->coordenada );
		$dtoEvent->setDate ( $evento->fecha);
		$dtoEvent->setContadorVisitas ( $evento->numVisitas );
		$dtoEvent->setHora ( $evento->hora );
		
		return $dtoEvent;
	}
	public function fromEventArray2DTOEvent($evento) {
		//print_r($evento);
		$dtoEvent = $this->createDTOEvent ();
		
		$dtoAreaAdmin = $this->createDTOAreaAdministrativa();
		$dtoAreaAdmin->setNameArea( $evento ["nombreAreaAdministrativa"] );
		$dtoAreaAdmin->setIdArea( $evento ["idAreaAdministrativa"] );
		
		$dtoEvent->setIdEvent ( $evento ["idEvento"] );
		$dtoEvent->setIdCreator ( $evento ["idCreador"] );
		$dtoEvent->setCategory ( $evento ["idCategoria"] );
		//$dtoEvent->setAreaAdmin ($dtoAreaAdmin->getNameArea() );
		//$dtoEvent->setIdAreaAdmin ($dtoAreaAdmin->getIdArea() );
		$dtoEvent->setAreaAdmin($dtoAreaAdmin);
		$dtoEvent->setStatus ( $evento ["estado"] );
		$dtoEvent->setNameEvent ( $evento ["nombreEvento"] );
		$dtoEvent->setDescription ( $evento ["descripcionEvento"] );
		$dtoEvent->setImage ( $evento ["imagenEvento"] );
		$dtoEvent->setCoord ( $evento ["coordenada"] );
		$dtoEvent->setDate ( $evento ["fecha"] );
		$dtoEvent->setContadorVisitas ( $evento ["numvisitas"] );
		$dtoEvent->setHora ( $evento ["hora"] );
		$dtoEvent->setNameCreator ( $evento ["nameCreator"] );
		$dtoEvent->setNameCategory ( $evento ["nombreCategoria"] );
		$listaDtoEvento [] = $dtoEvent;
		
		return $dtoEvent;
	}
	public function fromObjectList2DTOEventList($listaObjetos){
		$listaDTOEvents=array();
		foreach($listaObjetos as $evento){
			$dtoEvent = $this->fromEventTable2DTOEvent($evento);
			
			$listaDTOEvents[]=$dtoEvent;
		}
		return $listaDTOEvents;
	}
	public function fromEventArrayList2DTOEventList($listaObjetos){
		$listaDTOEvents=array();
		foreach($listaObjetos as $evento){
			$dtoEvent = $this->fromEventArray2DTOEvent($evento);
			
			$listaDTOEvents[]=$dtoEvent;
		}
		return $listaDTOEvents;
	}
	
	public function JSONEvent2DTOEvent($eventTmp){
		$eventDto = $this->createDTOEvent ();
		$eventDto->setContadorVisitas ( $eventTmp->_contadorVisitas );
		$eventDto->setIdCreator ( $eventTmp->_idCreator );
		$eventDto->setCategory ( $eventTmp->_category );
		$eventDto->setCoord ( $eventTmp->_coord );
		$eventDto->setAreaAdmin ( $eventTmp->_areaAdmin );
		$eventDto->setDate ( $eventTmp->_date );
		$eventDto->setHora ( $eventTmp->_hora );
		$eventDto->setDescription ( $eventTmp->_description );
		$eventDto->setIdEvent ( $eventTmp->_idEvent );
		$eventDto->setImage ( $eventTmp->_image );
		$eventDto->setNameEvent ( $eventTmp->_nameEvent );
		$eventDto->setStatus ( $eventTmp->_status );
		
		return $eventDto;
	}

	public function JSONUpdateEvent2DTOEvent($eventdb,$eventTmp,$idUser){
		$eventDto = $this->createDTOEvent ();	
		
		$eventDto->setContadorVisitas ( $eventdb->getContadorVisitas());
		//$eventDto->setIdCreator ( $eventTmp->_idCreator );
		$eventDto->setIdCreator ( $idUser );
		$eventDto->setCategory ( $eventTmp->_category );
		$eventDto->setCoord ( $eventdb->getCoord());
		$eventDto->setAreaAdmin ( $eventdb->getAreaAdmin());
		$eventDto->setDate ( $eventTmp->_date );
		$eventDto->setHora ( $eventTmp->_hora );
		$eventDto->setDescription ( $eventTmp->_description );
		$eventDto->setIdEvent ( $eventdb->getIdEvent());
		$eventDto->setImage ( $eventTmp->_image );
		$eventDto->setNameEvent ( $eventTmp->_nameEvent );
		$eventDto->setStatus ( $eventdb->getStatus());
				
		return $eventDto;
	}
	public function JSONEventSuscription2DTOEvent($eventSuscription,$idUser){
		$eventSuscrDto = $this->createDTOEventSuscription();
		$eventSuscrDto->setIdEvent($eventSuscription->_idEvent);
		$eventSuscrDto->setIdUser($idUser);
		
		return $eventSuscrDto;
	} 
	
	public function fromDTOEventSuscription2EventTable($evento){
		$row =&JTable::getInstance('suscripcionevento','Table');
		
		$row->idSuscripcionEvento = $evento->getIdEventSuscription();
		$row->idUsuario = $evento->getIdUser();
		$row->idEvento = $evento->getIdEvent();

		return $row;
	}
	
	public function fromEventSuscriptionTable2DTOEventSuscription($listaEventosSuscritos)
	{
		$listaDtoEventSuscription=array();
		foreach ($listaEventosSuscritos as $eventoSuscripto )
		{
			$dtoEventSuscription=$this->createDTOEventSuscription();
			$dtoEventSuscription->setIdEventSuscription($eventoSuscripto->idSuscripcionEvento);
			$dtoEventSuscription->setIdUser($eventoSuscripto->idUsuario);
			$dtoEventSuscription->setIdEvent($eventoSuscripto->idEvento);
		}
		
		return $listaDtoEventSuscription;
	}
	public function fromEventSubscriptionObject2Table($eventSubscriptionObject){
		$row =&JTable::getInstance('suscripcionEvento','Table');
		$row->idSuscripcionEvento = $eventSubscriptionObject->idSuscripcionEvento;
		$row->idEvento = $eventSubscriptionObject->idEvento;
		$row->idUsuario = $eventSubscriptionObject->idUsuario;
		return $row;
	}
	
	/*public function fromEventSuscriptionList2Table($suscripsionsEvent){
		//al ser multi no eestoy seguro si se hace asi, o hay que hacer tipo foreach....
		//igual al final no sirve de nada esto!
		$row =&JTable::getInstance('suscripcionEvento','Table');
		$row->idSuscripcionEvento = $eventSubscriptionObject->idSuscripcionEvento;
		$row->idEvento = $eventSubscriptionObject->idEvento;
		$row->idUsuario = $eventSubscriptionObject->idUsuario;
		return $row;
	}*/
	
	public function fromEventObject2Table($event){
		$row =&JTable::getInstance('evento','Table');
		$row->idEvento = $event->idEvento;
		return $row;
	}
	
	public function fromObjectList2DTOComentarioList($listaObjetos){
		$listaDTOComentario=array();
		foreach($listaObjetos as $comentario){
			$dtoComentario = $this->fromEventTable2DTOComentario($comentario);
			$listaDTOComentarios[]=$dtoComentario;
		}
		return $listaDTOComentarios;
	}
	
	public function fromEventTable2DTOComentario ($comentario){
		$commentDto = $this->createDTOEventComment();
		$commentDto->setIdComment ( $comentario->idComentario );
		$commentDto->setDate ( $comentario->fecha );
		$commentDto->setComment ( $comentario->descComentario );
		$commentDto->setIdUser($comentario->idUsuario);
		$commentDto->setNameUser(JFactory::getUser($comentario->idUsuario)->name);
		$commentDto->setIdEvento($comentario->idEvento);
		return $commentDto;
	}
	
	public function JSONEventComment2DTOEventComment ($eventComment){
		$commentDto = $this->createDTOEventComment ();
		$commentDto->setDate (JFactory::getDate()->toFormat());
		$commentDto->setComment ( $eventComment->_descComentario );
		$commentDto->setIdUser($eventComment->_idUsuario);
		$commentDto->setIdEvento($eventComment->_idEvento);
		
		return $commentDto;
	}
	
	public function fromDTOEventComment2EventCommentTable($eventDto){
		$row =&JTable::getInstance('comentario','Table');
		
		$row->idUsuario = $eventDto->getIdUser();
		$row->idEvento = $eventDto->getIdEvento();
		$row->descComentario = $eventDto->getComment();
		$row->fecha = $eventDto->getDate();
		
		return $row;
	}
	
	public function fromEventTable2DTOCategoria($categoria){
		$categoriaDto = $this->createDTOEventCategory();
		$categoriaDto->setIdCategory ( $categoria->idCategoria );
		$categoriaDto->setNameCategory ( $categoria->nombreCategoria );
		$categoriaDto->setDescription ( $categoria->descripcionCategoria );
		$categoriaDto->setImage($categoria->imagenCategoria);
		return $categoriaDto;
	}
	
	public function fromObjectList2DTOCategoryList($listaCategorias){
		$listaDTOCategoria=array();
		foreach($listaCategorias as $categoria){
			$dtoCategoria = $this->fromEventTable2DTOCategoria($categoria);
			$listaDTOCategoria[]=$dtoCategoria;
		}
		/*foreach ($listaDTOCategoria as $categoria){
			fputs($fp, $categoria->getNameCategory());
		}
		fclose($fp);*/
		return $listaDTOCategoria;
	}
}

?>